//
//  FirstPageViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "FirstPageViewController.h"
#import "JingxuanViewController.h"
#import "LiangpinViewController.h"
#import "HaodianViewController.h"
#import "YijiaViewController.h"
#import "ShuwuViewController.h"
#import "ChuwuViewController.h"

@interface FirstPageViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *bigScrollView;
@property (nonatomic, strong) UIScrollView *smallScrollView;
@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) NSInteger number;

@end

@implementation FirstPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.number = 0;
    self.array = [NSMutableArray array];
    self.buttonArray = [NSMutableArray array];
    
    //导航栏标题
    self.navigationItem.title = @"首页";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏右侧按钮 夜间模式
    UIImage *rightImage = [UIImage imageNamed:@"iconfont-sousuo.png"];
    //取消图片渲染
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:rightImage style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemAction:)];

    self.titleArray = @[@"精选",@"良品",@"好店",@"易家",@"书屋",@"厨务"];
    
    self.smallScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTHSCALE, 30 * HEIGHTSCALE)];
    self.smallScrollView.contentSize=CGSizeMake (75 * WIDTHSCALE * self.titleArray.count, 0);
    [self.smallScrollView setBounces:NO];//滚到边缘没有反弹效果
    [self.smallScrollView setPagingEnabled:NO];//不按页翻
    self.smallScrollView.showsHorizontalScrollIndicator = NO;
    self.smallScrollView.panGestureRecognizer.delaysTouchesBegan = YES;//拖拽手势
    self.smallScrollView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.smallScrollView];

    for (int i = 0; i < self.titleArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(75 * i * WIDTHSCALE, 0, 75 * WIDTHSCALE, 30 *HEIGHTSCALE);
        [button setTag:i];//设置标记
        [button setTitle:[self.titleArray objectAtIndex:i] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [self.smallScrollView addSubview:button];
        //将按钮加入数组
        [self.buttonArray addObject:button];
        //刚打开应用,显示第一个button就是红色且稍微变大
        if (i == 0) {
            [button setTitleColor:RGBA(251, 106, 10, 1) forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:19]];
        }
    }
    
    
    //页面
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30 *HEIGHTSCALE , 375 * WIDTHSCALE, (667 - 113) * HEIGHTSCALE)];
    [self.bigScrollView setContentSize:CGSizeMake(WIDTHSCALE * 375 * self.titleArray.count, 0)];
    [self.bigScrollView setBounces:NO];
    [self.bigScrollView setPagingEnabled:YES];//按页翻
    [self.bigScrollView setDelegate:self];
    [self.bigScrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:self.bigScrollView];
    
    
    JingxuanViewController *JingxuanVC = [[JingxuanViewController alloc]init];
    JingxuanVC.view.tag = 1000;
    JingxuanVC.view.frame = CGRectMake(0, 0, 375 * WIDTHSCALE, 667 * HEIGHTSCALE);
    [self addChildViewController:JingxuanVC];
    [self.bigScrollView addSubview:JingxuanVC.view];
    
    LiangpinViewController *LiangpinVC = [[LiangpinViewController alloc]init];
    LiangpinVC.view.tag = 1001;
    LiangpinVC.view.frame = CGRectMake(375 * WIDTHSCALE, 0, 375 * WIDTHSCALE, 667 *HEIGHTSCALE);
    [self addChildViewController:LiangpinVC];
    [self.bigScrollView addSubview:LiangpinVC.view];
    
    HaodianViewController *HaodianVC = [[HaodianViewController alloc]init];
    HaodianVC.view.tag = 1002;
    HaodianVC.view.frame = CGRectMake(375 * 2 * WIDTHSCALE, 0, 375 * WIDTHSCALE, 667 *HEIGHTSCALE);
    [self addChildViewController:HaodianVC];
    [self.bigScrollView addSubview:HaodianVC.view];
    
    YijiaViewController *YijiaVC = [[YijiaViewController alloc]init];
    YijiaVC.view.tag = 1003;
    YijiaVC.view.frame = CGRectMake(375 * 3 * WIDTHSCALE, 0, 375 * WIDTHSCALE, 667 *HEIGHTSCALE);
    [self addChildViewController:YijiaVC];
    [self.bigScrollView addSubview:YijiaVC.view];
    
    ShuwuViewController *ShuwuVC = [[ShuwuViewController alloc]init];
    ShuwuVC.view.tag = 1004;
    ShuwuVC.view.frame = CGRectMake(375 * 4 * WIDTHSCALE, 0, 375 * WIDTHSCALE, 667 *HEIGHTSCALE);
    [self addChildViewController:ShuwuVC];
    [self.bigScrollView addSubview:ShuwuVC.view];
    
    ChuwuViewController *ChuwuVC = [[ChuwuViewController alloc]init];
    ChuwuVC.view.tag = 1005;
    ChuwuVC.view.frame = CGRectMake(375 * 5 * WIDTHSCALE, 0, 375 * WIDTHSCALE, 667 *HEIGHTSCALE);
    [self addChildViewController:ChuwuVC];
    [self.bigScrollView addSubview:ChuwuVC.view];

   

}

//点击事件
-(void)buttonClick:(UIButton *)sender
{
//    self.number = button.frame.origin.x / (75 * WIDTHSCALE);
//    if (self.number==1)
//    {
//        DanpingViewController *baseVC=[self.childViewControllers objectAtIndex:self.number];
//        
//        [baseVC lscGetAFNetWorkingData];
//
//    }else if (self.number==2)
//    {
//        RemenViewController *baseVC=[self.childViewControllers objectAtIndex:self.number];
//        [baseVC lscGetAFNetWorkingData];
//    }else if (self.number==3)
//    {
//        YijiaViewController *baseVC=[self.childViewControllers objectAtIndex:self.number];
//        [baseVC lscGetAFNetWorkingData];
//    }else if (self.number==4)
//    {
//        WenxinshuwuViewController *baseVC=[self.childViewControllers objectAtIndex:self.number];
//        [baseVC lscGetAFNetWorkingData];
//    }else if (self.number==5)
//    {
//        JiatingchuwuViewController *baseVC=[self.childViewControllers objectAtIndex:self.number];
//        [baseVC lscGetAFNetWorkingData];
//    }
    
    
    for (UIButton *button in self.buttonArray) {
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:17]];
    }
    //点击当前按钮时,按钮文字变红且变大
    [sender setTitleColor:RGBA(251, 106, 10, 1) forState:UIControlStateNormal];
    [sender.titleLabel setFont:[UIFont systemFontOfSize:19]];
    
    //前三个button不移动位置
    if (sender.frame.origin.x == 0 || sender.frame.origin.x == 75 * WIDTHSCALE || sender.frame.origin.x == 75 * 2 * WIDTHSCALE) {
        
        [self.smallScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
    }//后三个button保持原位
    else if (sender.frame.origin.x == (self.buttonArray.count - 2) * 75 * WIDTHSCALE || sender.frame.origin.x == (self.buttonArray.count - 1 ) * 75 *WIDTHSCALE || sender.frame.origin.x == (self.buttonArray.count - 0) * 75 *WIDTHSCALE)
    {
        [self.smallScrollView setContentOffset:CGPointMake((self.buttonArray.count - 5) * 75 * WIDTHSCALE, 0) animated:YES];
    }//其他按位置移动
    else {
        
        [self.smallScrollView setContentOffset:CGPointMake(75 * WIDTHSCALE, 0) animated:YES];
    }
    
    //页面相应偏移
    [self.bigScrollView setContentOffset:CGPointMake(sender.tag * 375 * WIDTHSCALE, 0) animated:YES];
 
}

//减速停止时触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.bigScrollView == scrollView)
    {
        for (int i = 0; i <self.buttonArray.count; i++)
        {
            UIButton *button = [self.buttonArray objectAtIndex:i];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:17]];
        }
        
        //对应按钮文字变红且变大
        UIButton *button = [self.buttonArray objectAtIndex:self.bigScrollView.contentOffset.x / (375 * WIDTHSCALE)];
        [button setTitleColor:RGBA(251, 106, 10, 1) forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:19]];
        
        //前三页button不移动位置
        if (self.bigScrollView.contentOffset.x == 0 || self.bigScrollView.contentOffset.x / (375 * WIDTHSCALE) == 1 || self.bigScrollView.contentOffset.x / (375 * WIDTHSCALE) == 2) {
            
            [self.smallScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            
        }//后三个button保持原位
        else if (self.bigScrollView.contentOffset.x / (375 * WIDTHSCALE) == 4 || self.bigScrollView.contentOffset.x / (375 * WIDTHSCALE) == 5 || self.bigScrollView.contentOffset.x / (375 * WIDTHSCALE) == 6)
        {
            [self.smallScrollView setContentOffset:CGPointMake((self.buttonArray.count- 5) * 75 * WIDTHSCALE, 0) animated:YES];
        }//其他按位置移动
        else {
            
            [self.smallScrollView setContentOffset:CGPointMake(75 * WIDTHSCALE, 0) animated:YES];
        }  
        
    }
}


//搜索
-(void)rightBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
//    SearcherNetViewController *searchVC=[[SearcherNetViewController alloc]init];
//    [searchVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
//    searchVC.hidesBottomBarWhenPushed=YES;
//    searchVC.navigationItem.title=@"搜索";
//    [self.navigationController pushViewController:searchVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
